import axios from "axios";
import qs from "querystring";

// 设置超时时间
const http = axios.create({ timeout: 10000 });

// 设置默认请求头
http.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded";
// 设置默认数据格式
http.defaults.transformRequest = (data) => qs.stringify(data);

// 请求拦截
http.interceptors.request.use(
  (config) => config,
  (error) => Promise.error(error)
);

// 响应拦截
http.interceptors.response.use(
  // 请求成功
  (res) => (res.status === 200 ? Promise.resolve(res) : Promise.reject(res)),
  // 请求失败
  (error) => Promise.reject(error.response)
);

export default http;
